package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dao.CompanyDao;
import com.wanxi.dao.impl.CompanyDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.CompanyModel;
import com.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyDaoImpl();

    @Override
    public CompanyModel getCompanyModel() {

        /**
         * 怎么拿到redis？
         * 1.导包
         * 2.加载驱动
         * 3.得到连接
         */
        Jedis jedis = new Jedis();
        CompanyModel companyModel = new CompanyModel();
        boolean isHave = jedis.exists("logo");
        if (isHave) {
//            能不能将companyModel转换为json字符串，存入到redis，

//            然后将redis里面的数据取出来强制转换为companyModel
//            companyModel =parseObject("companyModel")
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setVersion(jedis.get("version"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setEmail(jedis.get("email"));
        } else {
            companyModel = companyDao.getCompanyModel();
//            jedis.set("companyModel",JSONObject.toJSONString(companyModel));
            jedis.set("logo", companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("address", companyModel.getAddress());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("version", companyModel.getVersion());
            jedis.set("linePhone", companyModel.getLinePhone());
        }
        return companyModel;
    }

//    public ResultDTO update(CompanyModel companyModel) {
//        int count = companyDao.update(companyModel);
//        if (count > 0) {
//            Jedis jedis = new Jedis();
//            jedis.del("logo");
//        }
//        return ResultDTO.getDTO(count, null);
//    }
}
